package com.buybuyyi.stylistcrm.resource.goods.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.buybuyyi.stylistcrm.object.util.BeanTranslater;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsBrandDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsClassIficationsDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsMaterialBrandDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsMaterialDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsTagsDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsYardageDao;
import com.buybuyyi.stylistcrm.resource.goods.po.Goods;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsBrand;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsClassIfications;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsMaterial;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsMaterialBrand;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsTags;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsYardage;

import stylistcrm.core.query.ResultPagerModel;
import stylistcrm.core.services.goods.dto.*;
import stylistcrm.core.services.goods.service.IGoodsService;
import stylistcrm.core.services.user.service.UserService;

@Service
public class GoodsService implements IGoodsService{
    
	@Autowired
	GoodsDao goodsDao;
	
	@Autowired
	GoodsBrandDao goodsBrandDao;
	
	@Autowired
	GoodsClassIficationsDao goodsClassIficationsDao;
	
	@Autowired
	GoodsTagsDao goodsTagsDao;
	
	@Autowired
	GoodsMaterialDao goodsMaterialDao;
	
	@Autowired
	GoodsMaterialBrandDao goodsMaterialBrandDao;
	
	@Autowired
	GoodsYardageDao goodsYardageDao;
	
	@Autowired
	UserService userService;

	@Transactional
	public GoodsResultDTO saveGoods(GoodsDTO goodDto) throws Exception {
		GoodsResultDTO resultDto=new GoodsResultDTO();
		//保存商品
		Goods goods=new Goods();
		goods.setGoodsCheckStatus(0L);
		BeanTranslater.copyProperties(goodDto, goods);
		Long goodsId=goodsDao.insert(goods);
		//保存商品品牌表
		GoodsBrand record =new GoodsBrand();
		record.setBrandId(goodDto.getDicBrandDto().get(0).getId());
		record.setGoodsId(goodsId);
		goodsBrandDao.insert(record);
		//保存商品标签表
		GoodsTags goodTages=new GoodsTags();
		goodTages.setGoodsId(goodsId);
		goodTages.setTagId(goodDto.getDicTagsDto().get(0).getId());
		goodsTagsDao.insert(goodTages);
		//保存商品类别
		GoodsClassIfications goodClassFication=new GoodsClassIfications();
		goodClassFication.setGoodsId(goodsId);
		if(goodDto.getDicClassificationsDto()!= null && goodDto.getDicClassificationsDto() != null){
			goodClassFication.setClassificationId(goodDto.getDicClassificationsDto().get(0).getId());
			goodsClassIficationsDao.insert(goodClassFication);
		}
		//保存商品面料以及材质
		List<DicMaterialCottonDTO> dicMaterialCottonDtos=goodDto.getDicMaterialCottonDtos();
		if(null != dicMaterialCottonDtos && dicMaterialCottonDtos.size()>0){
			for(DicMaterialCottonDTO cottonDto : dicMaterialCottonDtos){
				GoodsMaterial goodMaterial=new GoodsMaterial();
				goodMaterial.setGoodsId(goodsId);
				goodMaterial.setMaterialId(cottonDto.getId());
				goodMaterial.setType(Long.parseLong(cottonDto.getType()));
				goodsMaterialDao.insert(goodMaterial);
			}
		}
		//面料品牌
		GoodsMaterialBrand materialBrand=new GoodsMaterialBrand();
		materialBrand.setGoodsId(goodsId);
		if(null !=goodDto.getGoodsMaterialBrandDto() && null != goodDto.getGoodsMaterialBrandDto().get(0)){
			materialBrand.setMaterialBrandId(goodDto.getGoodsMaterialBrandDto().get(0).getId());
			goodsMaterialBrandDao.insert(materialBrand);
		}
		//保存库存
		List<GoodsColorDto> goodsYarDageDtos=goodDto.getGoodsYadageDtos();
		if(null != goodsYarDageDtos && goodsYarDageDtos.size()>0){
			for(GoodsColorDto yarDage : goodsYarDageDtos){
				List<GoodsSizeDTO> sizeDtos=yarDage.getSize();
				for(GoodsSizeDTO size :sizeDtos ){
					GoodsYardage yarDageBean=new GoodsYardage();
					yarDageBean.setGoodsId(goodsId);
					yarDageBean.setColor(yarDage.getName());
					yarDageBean.setColorvalue(yarDage.getName());
					yarDageBean.setSize(size.getName());
					yarDageBean.setStockQuantity(size.getNum());
					goodsYardageDao.insert(yarDageBean);
				}

			}
		}
		resultDto.setCode("1");
		resultDto.setMessage("保存成功");
		return resultDto;
		
	}

	public List<GoodsDTO> getGoodsByContation() throws Exception {
		return null;
	}

	@Override
	public  ResultPagerModel<GoodsDTO> getGoodsByContation(GoodsPageDTO goodPageDto) throws Exception {
		ResultPagerModel<GoodsDTO> resultPageModels=new ResultPagerModel<GoodsDTO>();
		//分页查询商品
		List<Goods> goodsList=goodsDao.getPageList(goodPageDto);
		List<GoodsDTO> goodDtos=new ArrayList<>();
		BeanTranslater.copyProperties(goodsList, goodDtos);
		//大小
		Integer size=goodsDao.getPageListSize(goodPageDto);
		resultPageModels.setCurrentPage(goodPageDto.getPageNo());
		resultPageModels.setPageSize(goodPageDto.getPageSize());
		resultPageModels.setTotalServerItems(size);
		resultPageModels.setData(goodDtos);
		return resultPageModels;
	}

	@Override
	public GoodsResultDTO changeGoodsStatus(GoodsYardageChangeDto yardageDao) throws Exception {
		GoodsResultDTO resultDto=new GoodsResultDTO();
		resultDto.setCode("0");
		//查询商品是否存在
		Goods goods=goodsDao.selectByPrimaryKey(yardageDao.getGoodId());
		if(null == goods){
			resultDto.setMessage("商品不存在");
			return resultDto;
		}
		return null;
	}
	

	@Transactional
	@Override
	public GoodsResultDTO modifyGoodsStatus(GoodsDTO goodDto) throws Exception {
		GoodsResultDTO resultDto=new GoodsResultDTO();
		resultDto.setCode("0");
		//查询商品是否存在
		Goods goods=goodsDao.selectByPrimaryKey(goodDto.getId());
		if(null == goods){
			resultDto.setMessage("商品不存在");
			return resultDto;
		}
		//修改商品的状态
		goods.setGoodsCheckStatus(goodDto.getGoodsCheckStatus());
		goodsDao.updateByPrimaryKey(goods);
		resultDto.setCode("1");
		resultDto.setMessage("修改成功");
		return resultDto;
	}

	@Override
	public GoodsResultDTO changeGoodsYardage(GoodsDTO goodDto) throws Exception {
		GoodsResultDTO resultDto=new GoodsResultDTO();
		resultDto.setCode("0");
		//查询商品是否存在
		Goods goods=goodsDao.selectByPrimaryKey(goodDto.getId());
		if(null == goods){
			resultDto.setMessage("商品不存在");
			return resultDto;
		}
		//修改存在的库存，添加不存在的库存
		//先删除存在的库存
		goodsYardageDao.deleteByPrimaryKey(goods.getId());
		//保存库存
		List<GoodsColorDto>  goodsYarDageDtos=goodDto.getGoodsYadageDtos();
		if(null != goodsYarDageDtos){
			for(GoodsColorDto yarDage : goodsYarDageDtos){
				List<GoodsSizeDTO> sizeDtos=yarDage.getSize();
				for(GoodsSizeDTO size :sizeDtos ){
					GoodsYardage yarDageBean=new GoodsYardage();
					yarDageBean.setGoodsId(goodDto.getId());
					yarDageBean.setColor(yarDage.getName());
					yarDageBean.setColorvalue(yarDage.getName());
					yarDageBean.setSize(size.getName());
					yarDageBean.setStockQuantity(size.getNum());
					goodsYardageDao.insert(yarDageBean);
				}
			}
		}
		resultDto.setCode("1");
		resultDto.setMessage("保存成功");
		return resultDto;
	}
}
